<?php
/**
 * 日期间隔
 */
$this->title = 'My Yii Application';
$css = <<<CSS
.sm{width:4.3rem;text-align:center;border-radius: 4px;border:1px solid gray;padding: 2px}
select{width: 5rem!important;}
.moreheight{margin-top: 6px}
CSS;

$this->registerCss($css);

$js = <<<JS

    var date=document.querySelector('#todaydl').innerText.split('-');
    var lunar=calendar.solar2lunar(date[0],date[1],date[2])
    
    $("#sy,#sy3").val(date[0]);
    $("#sy2").val(date[0]-0+1);
    $("#sm,#sm2,#sm3").val(date[1]);
    $("#sd,#sd2,#sd3").val(date[2]);
        
        
    console.log(lunar);
    function formatLun(lunar){
        var str='农历'+lunar.IMonthCn+lunar.IDayCn+'&nbsp;'+lunar.gzYear+'年('+lunar.Animal+')&nbsp;'+lunar.gzMonth+'月&nbsp;'+lunar.gzDay+'日&nbsp;'+lunar.ncWeek;
        if (lunar.Term!=null){
            str +=' &nbsp;'+lunar.Term;
        }
        if (lunar.lunarFestival!=null){
            str +=' &nbsp;'+lunar.lunarFestival;
        }
        
        return str;
    }
    document.querySelector('#today').innerHTML=formatLun(lunar);
    
    
    
    var hzWeek= new Array("日","一","二","三","四","五","六","日");
function cweekday(wday){return hzWeek[wday];}

var nStr1=new Array('','一','二','三','四','五','六','七','八','九','十','十一','十二');
var nStr2=new Array('初','十','廿','卅','□');
Animals=new Array("鼠","牛","虎","兔","龙","蛇","马","羊","猴","鸡","狗","猪");
function GetcDay(d){var s;switch(d){case 10:s='初十';break;case 20:s='二十';break;case 30:s='三十';break;default:s=nStr2[Math.floor(d/10)];s+=nStr1[d%10];break;}return(s);}
function GetcMon(m){
     if(m<0){ret="闰";m*=-1;} else ret="";
     return ret+(m==1?'正':nStr1[m]);
}


function getdatestring( dobj)
{
    
    var lunar=calendar.solar2lunar(dobj.getFullYear(),1+dobj.getMonth(),dobj.getDate());
    
    var str='农历'+lunar.IMonthCn+lunar.IDayCn+'&nbsp;'+lunar.gzYear+'年('+lunar.Animal+')&nbsp;'+lunar.gzMonth+'月&nbsp;'+lunar.gzDay+'日';
        if (lunar.Term!=null){
            str +=' &nbsp;'+lunar.Term;
        }
        if (lunar.lunarFestival!=null){
            str +=' &nbsp;'+lunar.lunarFestival;
        }
    
return dobj.getFullYear()+"年"+(1+dobj.getMonth())+"月"+dobj.getDate()+"日"+"星期" +cweekday(dobj.getDay())+"<br />"+str;

}

    function dayadd()
    {
        var sy=parseInt($("#sy3").val());
        var sm=parseInt($("#sm3").val())-1;
        var sd=parseInt($("#sd3").val());
        
        var day0=new Date(sy,sm,sd);
        
        if(day0=="Invalid Date"){
            alert("请输入正确日期");
            return;
        }

        var dnum=parseInt($("#val").val())*$("#pom").val();
        var dtype=$("#dtype").val();
        
        if(dtype==1){
            day1=new Date(sy,sm,sd+dnum);
        }
        if(dtype==2){
            day1=new Date(sy,sm,sd+dnum*7);
        }
        else if(dtype==3){
            day1=new Date(sy,sm+dnum,sd);
        }


        $("#resultadd").html(getdatestring(day1));
        


    }


    $('#daydiff').on('click',daydiff);
    $('#dayadd').on('click',dayadd);
    
    function daydiff()
    {
 
        var y2=$("#sy").val();
        var m2=$("#sm").val();
        var d2=$("#sd").val();
        
        var y3=$("#sy2").val();
        var m3=$("#sm2").val();
        var d3=$("#sd2").val();
        
        var day2=new Date(y2,m2-1,d2);
        var day3=new Date(y3,m3-1,d3);
        
        var dnum=(day3-day2)/86400000;
        
        var wnum=0;
        var wleft=0;
        var mnum=0;
        var mleft=0;
    
        if(dnum>0){
            wnum=Math.floor(dnum/7);
            wleft=dnum%7;
            
            mnum=(y3-y2)*12+(m3-m2);
            if(d3>=d2){
                mleft=d3-d2;
            }
        }else{
            mnum--;
            daytemp=new Date(y3,m3-2,d2);
            mleft=(day3-daytemp)/86400000;
        }
    
    
        wstr2=wleft>0?wleft+"天":"";
        wstr1=wnum>0?" = "+wnum+"星期"+wstr2:"";
        mstr2=mleft>0?mleft+"天":"";
        mstr1=mnum>0?" = "+mnum+"个月"+mstr2:"";
        
        $("#result").html(dnum+"天"+wstr1+mstr1);   
    
    }




JS;
$this->registerJs($js);

$this->registerJsFile('/date/calendar.js');
?>

<div class="site-index">
    <div class="body-content">
        <h3>日期计算器</div><hr/>
        <span class="col-lg-12">
            <div class="form-group">
                    <span>
                        <label>现在是：</label><span id="todaydl"><?php echo date('Y-m-d')?></span>
                        <span id="today" class="col-sm-12"></span>
                    </span>
            </div>

       <hr/>
        <h4>日期间隔计算</h4><hr/>
         <form class="form-inline ">
                <div class="form-group col-lg-12 col-md-12 col-sm-12 col-xs-12">
                    <div class=" col-xs-12 text-center " >
                        <span >开始日期</span>
                            <input id="sy" type="number" class="sm" />年
                            <input id="sm" type="number" class="sm" min="1" max="12" />月
                            <input id="sd" type="number" class="sm" min="1" max="31" />日
                    </div>

                    <div class="col-xs-12  text-center moreheight">
                        <span >结束日期</span>
                            <input id="sy2" type="number" class="sm" />年
                            <input id="sm2" type="number" class="sm" min="1" max="12"/>月
                            <input id="sd2" type="number" class="sm" min="1" max="31"/>日
                    </div>
                </div>
                <div class="col-xs-12 col-lg-12 col-md-12 text-center" style="margin-top: 4px">
                        <a id="daydiff" class="btn btn-primary col-lg-4 col-xs-6" style="float: initial !important;">
                            <i   class="icon-chevron-down icon-white"></i>求间隔</a>
                 </div>
                <div class="col-xs-12 col-lg-12 col-md-12 text-center moreheight" id="result" style="height: 4rem;text-height:4rem"></div>
            </form>

            <hr/>
         <h4>推算间隔日期</h4><hr/>
         <form class="form-inline ">
                <div class="form-group col-lg-12 col-md-12 col-sm-12 col-xs-12">
                    <div class=" col-xs-12 text-center">
                        <span >指定日期</span>
                            <input id="sy3" type="number" class="sm" />年
                            <input id="sm3" type="number" class="sm" min="1" max="12" />月
                            <input id="sd3" type="number" class="sm" min="1" max="31" />日
                    </div>

                    <div class="col-xs-12  text-center moreheight">
                        <select id="pom" class="sm">
                        <option value="1">往后</option>
                        <option value="-1">往前</option>
                        </select>
                        <input id="val" type="number" class="sm" value="1" />
                        <select id="dtype" class="sm">
                            <option value="1">天</option>
                            <option value="2">星期</option>
                            <option value="3">月</option>
                        </select>
                    </div>
                </div>

                <div class="col-xs-12 col-lg-12 col-md-12 text-center" style="margin-top: 4px">
                        <a id="dayadd" class="btn btn-primary col-lg-4 col-xs-6" style="float: initial !important;"><i
                                class="icon-chevron-down icon-white"></i>推算日期</a>
                 </div>

                <div class="col-xs-12 col-lg-12 col-md-12 text-center moreheight" id="resultadd" style="height: 4rem;text-height:4rem"></div>
            </form>


</div>

</div>

